﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace Uinside.CreditSystem.WPF.Tools
{
    public class IntBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is int))
                return false;

            var bindingValue = (int)value;
            int intValue;
            if (int.TryParse(parameter as string, out intValue))
            {
                if (intValue == bindingValue)
                    return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(!(value is bool))
                throw new Exception("value should be Boolean type");
            
            var bindingValue = (bool) value;
            if (bindingValue)
            {
                return int.Parse(parameter.ToString());
            }
            else
            {
                return 0;
            }
        }
    }
}
